﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace CrazyClient
{
    /// <summary>
    /// Interaction logic for MoveStraightIn.xaml
    /// </summary>
    public partial class RotateOut : UserControl, ITransition
    {
        public RotateOut()
        {
            InitializeComponent();
        }
        RotateTransform3D trans;
        string _name;
        private void GetTrans()
        {
            LyricPanel p = ParentPanel as LyricPanel;
            foreach (Transform3D t in p.Transition.Children)
            {
                if (t is RotateTransform3D )
                {
                    trans = (RotateTransform3D)t;
                    _name = "rot";
                }
            }
            if (trans == null)
            {
                trans = new RotateTransform3D();
                p.Transition.Children.Add(trans);
                _name = "rot";

                p.Parent.RegisterName(_name, trans);

            }
        }
        public void Go()
        {
            if (ParentPanel is LyricPanel)
            {
                _isDone = false;
                LyricPanel p = ParentPanel as LyricPanel;
                GetTrans();


                Storyboard sb = new Storyboard();
                sb.Completed += new EventHandler(sb_Completed);
                sb.Duration = Duration;

                //sb.FillBehavior = FillBehavior.Stop;
                // sb.RepeatBehavior = RepeatBehavior.Forever;

                Rotation3DAnimation dar = new Rotation3DAnimation(To, Duration);
               // DoubleAnimation day = new DoubleAnimation(To.Y, Duration);
                //DoubleAnimation daz = new DoubleAnimation(To.Z, Duration);
                //  dax.RepeatBehavior = RepeatBehavior.Forever;
                //  day.RepeatBehavior = RepeatBehavior.Forever;
                // daz.RepeatBehavior = RepeatBehavior.Forever;
                //  dax.IsAdditive = true;
                //  day.IsAdditive = true;
                //   daz.IsAdditive = true;

                Storyboard.SetTargetName(dar, _name);
                Storyboard.SetTargetProperty(dar, new PropertyPath(RotateTransform3D.RotationProperty));
            

              //  Storyboard.SetTargetName(day, _name);
              //  Storyboard.SetTargetProperty(day, new PropertyPath(TranslateTransform3D.OffsetYProperty));
              

               // Storyboard.SetTargetName(daz, _name);
              //  Storyboard.SetTargetProperty(daz, new PropertyPath(TranslateTransform3D.OffsetZProperty));
              
                sb.Children.Add(dar);
               // sb.Children.Add(day);
              //  sb.Children.Add(daz);

                
                //     sb.Completed += new EventHandler(anim_Completed);
                Resources.Add(Guid.NewGuid(), sb);

                sb.Begin(p.Parent);
                //Point3DAnimation anim = new Point3DAnimation(To, Duration);


                // trans.BeginAnimation(TranslateTransform3D.OffsetXProperty, anim, HandoffBehavior.Compose);

            }
        }

        void sb_Completed(object sender, EventArgs e)
        {
            LyricPanel p = ParentPanel as LyricPanel;
            if (p.TransitionIn.IsDone)
            {
                p.TransitionIn.Finish();
                p.Transition.Children.Remove(trans);
                p.Parent.UnregisterName(_name);
                trans = null;
                _isDone = false;
            }
            else
                _isDone = true;
        }

        public void Finish()
        {
            ////      LyricPanel p = ParentPanel as LyricPanel;
            //     p.Transition.Children.Remove(trans);
            trans = null;
            _isDone = false;
        }

        public bool IsDone
        {
            get
            {
                return _isDone;
            }
        }
        bool _isDone;
        void anim_Completed(object sender, EventArgs e)
        {
            //LyricPanel p = ParentPanel as LyricPanel;
            // p.TheContent.BeginAnimation(VisualBrush.OpacityProperty, null, HandoffBehavior.Compose);

        }

        #region ParentPanelProperty
        public static readonly DependencyProperty ParentPanelProperty =
            DependencyProperty.Register("ParentPanel", typeof(LyricPanel),
            typeof(MoveStraightOut),
            new PropertyMetadata(OnParentPanelChanged));

        static private void OnParentPanelChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public LyricPanel ParentPanel
        {
            get
            {
                object result = GetValue(ParentPanelProperty);
                return (LyricPanel)result;
            }
            set
            {

                SetValue(ParentPanelProperty, value);
            }
        }

        #endregion


        #region ToProperty
        public static readonly DependencyProperty ToProperty =
            DependencyProperty.Register("To", typeof(Rotation3D),
            typeof(MoveStraightOut),
            new PropertyMetadata(OnToChanged));

        static private void OnToChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public Rotation3D To
        {
            get
            {
                object result = GetValue(ToProperty);
                return (Rotation3D)result;
            }
            set
            {

                SetValue(ToProperty, value);
            }
        }

        #endregion


        #region FromProperty
        public static readonly DependencyProperty FromProperty =
            DependencyProperty.Register("From", typeof(Point3D),
            typeof(MoveStraightOut),
            new PropertyMetadata(OnFromChanged));

        static private void OnFromChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public Point3D From
        {
            get
            {
                object result = GetValue(FromProperty);
                return (Point3D)result;
            }
            set
            {

                SetValue(FromProperty, value);
            }
        }

        #endregion


        #region DurationProperty
        public static readonly DependencyProperty DurationProperty =
            DependencyProperty.Register("Duration", typeof(Duration),
            typeof(MoveStraightOut),
            new PropertyMetadata(OnDurationChanged));

        static private void OnDurationChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public Duration Duration
        {
            get
            {
                object result = GetValue(DurationProperty);
                return (Duration)result;
            }
            set
            {

                SetValue(DurationProperty, value);
            }
        }

        #endregion
    }
}
